class OperationDate {
	constructor(hook, moment, dates, format) {
		this.hook = hook;
		this.format = format;
		this.dates = [];
		this.moments = [];
		this.moment = moment;
		// ===============================================
		const { $ref } = hook.$m;
		this.responseDates = [$ref(null), $ref(null)];
		this.disabledDates = [$ref(null), $ref(null)];
		if (Array.isArray(dates) && dates.length) this.init(dates);
	}
	init(dates) {
		// 指定截至时间的可选范围
		this.initStart(dates[0]);
		// 指定开始时间的可选范围
		this.initEnd(dates[1]);
	}
	changeStart(e) {
		this.dates[0] = e ? e.format(this.format) : null;
		this.responseDates[0].value = this.moments[0] = e;
		this.initDisabled(this.disabledDates[1], 0, 'isAfter');
	}
	changeEnd(e) {
		this.dates[1] = e ? e.format(this.format) : null;
		this.responseDates[1].value = this.moments[1] = e;
		this.initDisabled(this.disabledDates[0], 1, 'isBefore');
	}
	// 已知开始时间，指定截至时间的可选范围
	initStart(dateStr = null) {
		this.dates[0] = dateStr;
		this.moments[0] = this.strToMoment(dateStr);
		this.responseDates[0].value = this.moments[0];
		this.initDisabled(this.disabledDates[1], 0, 'isAfter');
	}
	// 已知截至时间，指定开始时间的可选范围
	initEnd(dateStr = null) {
		this.dates[1] = dateStr;
		this.moments[1] = this.strToMoment(dateStr);
		this.responseDates[1].value = this.moments[1];
		this.initDisabled(this.disabledDates[0], 1, 'isBefore');
	}

	initDisabled(disbaledRef, index, type) {
		// 转化为moment对象用于比较
		const e = this.moments[index];
		disbaledRef.value = (day) => {
			const str = this.dates[index];
			if (str) {
				// 当天允许选中，采用字符串全等比较判定，其他时间使用moment比较
				return day.format(this.format) === str ? false : e[type](day);
			}
		};
	}

	strToMoment(str) {
		return str ? this.moment(str) : null;
	}
	// 时间向前推
	lastDay(mo, num = 1) {
		return this.moment(mo).subtract(num, 'day');
	}
	// 时间向后推
	nextDay(mo, num = 1) {
		return this.moment(mo).add(num, 'day');
	}
}

export default OperationDate;